var app = getApp()

Page({
  data: {
    total:0,
    items:[
      // {"good_id":"1","good_img":"/images/shouye_04.png","good_name":"产品1",
      // "good_old_price":"108","good_selling_price":"98","quantity":24,"good_is_checked":false},
      // {"good_id":"3","good_img":"/images/shouye_05.png","good_name":"产品2",
      // "good_old_price":"108","good_selling_price":"9","quantity":24,"good_is_checked":false}
    ],
    is_show_bottem:"false",
    is_show_top:"true",

  },
  onLoad(query) {
    // console.log(query);
    this.init();
  },


  async init(){
    // console.log('in cart init');
            var app = getApp()
            var url = app.globadData.apiUrlHeader+'get-cart'
            var id = this.data.items.goods_id
             var i = await app.getUserInfo();
     app.globadData.userInfo.nickName = i.data.nickName;
      console.log('3');
      app.globadData.userInfo.avatar = i.data.avatar;
      var j = await app.login(i.data.auth_code);
      console.log(j);
      app.globadData.access_token = j;
               my.httpRequest({
                   url: url, // 目标服务器 url
                   method:'Post',
                   headers:{'Content-Type':'Application/json'},
                   data:JSON.stringify({
                      "goods_id":id,
                      "access_token":app.globadData.access_token
                    }),
                  dataType:'json',
                  success: (res) => {
                  //  console.log(res);
                    
                    //  for(var i=0;i<res.data.data.length;i++){
                    //    res.data.data.quantity = parseInt(res.data.data.quantity);
                    //  }
                    var hidden_top = false;
                    var hidden_bottem = true;
                    if(res.data.status != 0){
                        hidden_top = true;
                        hidden_bottem = false;
                    }
                     this.setData({
                       items:res.data.data,
                       is_show_bottem : hidden_bottem,
                       is_show_top : hidden_top
                     });
                   },
               });

  },


  onCheckboxGroupChange(e){
    var index = e.currentTarget.dataset.index;
    var carts = this.data.items;
    
    var selected = carts[index].good_is_checked;
    carts[index].good_is_checked = !selected; 
    this.calTotal();
  },
  onAllChecked(e){
    var carts = this.data.items;
      for (var i = 0; i < carts.length; i++) {
        if(e.detail.value)
        carts[i].good_is_checked = true;
        else
        carts[i].good_is_checked = false;
      }
    this.setData({
           items : carts
        });
      this.calTotal();
    },
    delCart(){
        var carts = this.data.items;
        this.total = 0;
        var goods_ids = [];
        for (var i = 0; i < carts.length; i++) {
          
            if(carts[i].good_is_checked)
            {
                goods_ids [i] = carts[i].good_id;
            }
            // this.total = this.total + carts[i].good_selling_price * carts[i].quantity
          }
          if(goods_ids.length<1)
            return;
            console.log(goods_ids);
          var app = getApp()
            var url = app.globadData.apiUrlHeader+'del-carts'
               my.httpRequest({
                   url: url, // 目标服务器 url
                   method:'Post',
                   headers:{'Content-Type':'Application/json'},
                   data:JSON.stringify({
                      "goods_ids":goods_ids,
                      "access_token":app.globadData.access_token
                    }),
                  dataType:'json',
                  success: (res) => {
                     console.log(res.data);
                      my.showToast({
                        content: res.data.message, // 文字内容
                        success: (res) => {
                          
                        },
                      });
                    this.init();
                   },
               });
      },
    onJia(e){
       console.log('+');
       console.log(this.data.items);
       var index = e.currentTarget.dataset.index;
       var carts = this.data.items;
       console.log(this.data.items[index]);
       var qu = parseInt(this.data.items[index].quantity);
      //  var qu = carts[index].quantity;
       this.data.items[index].quantity = qu+1;

       this.setData({
         items : carts
        })
        this.calTotal();
      },
      
      onJian(e){
        console.log('-');
       var index = e.currentTarget.dataset.index;
       var carts = this.data.items;
       var qu = parseInt(carts[index].quantity);
       if(carts[index].quantity>0)
       carts[index].quantity = qu-1;

       this.setData({
         items : carts
        })
        this.calTotal();
      },

  calTotal(e){
       var carts = this.data.items;
       this.total = 0;
       for (var i = 0; i < carts.length; i++) {
          
            if(carts[i].good_is_checked)
            this.total = this.total + carts[i].good_selling_price * carts[i].quantity
         
        }
        this.setData({
          total:this.total
        })
      },
      submit(e){
        // console.log(e);
        // console.log(this);
        
        if(this.data.total<=0){
          my.showToast({
            content: '亲，购物车还没有东东哟，请继续选购吧！', // 文字内容
            success: (res) => {
              
            },
          });
        }else{//结算
            app.globadData.carts = this.data.items;
            

            my.redirectTo({
                 url: '/pages/main/settleAccount/settleAccount', // 需要跳转的应用内非 tabBar 的页面的路径，路径后可以带参数。参数与路径之间使用
                          });

            }

        
          },
                                
        goIndex(){
              my.redirectTo({
                 url: '/pages/main/index/index', // 需要跳转的应用内非 tabBar 的页面的路径，路径后可以带参数。参数与路径之间使用
                          });

            
        }

  
});
